<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 6.0.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.3/css/all.min.css" integrity="sha256-2H3fkXt6FEmrReK448mDVGKb3WW2ZZw35gI7vqHOE4Y=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"example.com","root":"/","images":"/images","scheme":"Gemini","version":"8.7.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":true,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"Searching...","empty":"We didn't find any results for the search: ${query}","hits_time":"${hits} results found in ${time} ms","hits":"${hits} results found"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>
<meta name="description" content="常用代码">
<meta property="og:type" content="article">
<meta property="og:title" content="code">
<meta property="og:url" content="http://example.com/2022/03/28/47/index.html">
<meta property="og:site_name" content="Xiaolong&#39;s Blog">
<meta property="og:description" content="常用代码">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://cdn.jsdelivr.net/gh/Drwna/image//images/Snipaste_2022-04-27_21-08-49.png">
<meta property="article:published_time" content="2022-03-28T14:03:47.000Z">
<meta property="article:modified_time" content="2023-02-20T09:17:09.442Z">
<meta property="article:author" content="Xiaolong">
<meta property="article:tag" content="js">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://cdn.jsdelivr.net/gh/Drwna/image//images/Snipaste_2022-04-27_21-08-49.png">


<link rel="canonical" href="http://example.com/2022/03/28/47/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"en","comments":true,"permalink":"http://example.com/2022/03/28/47/","path":"2022/03/28/47/","title":"code"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>code | Xiaolong's Blog</title>
  




  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">Xiaolong's Blog</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>Home</a></li>
        <li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>Tags</a></li>
        <li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>Archives</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>Search
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="Searching..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%88%B6%E5%85%83%E7%B4%A0"><span class="nav-number">1.</span> <span class="nav-text">父元素</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E7%88%B6%E5%85%83%E7%B4%A0-1"><span class="nav-number">1.0.1.</span> <span class="nav-text">父元素</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%AD%90%E5%85%83%E7%B4%A0"><span class="nav-number">1.0.2.</span> <span class="nav-text">子元素</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%9B%BE%E7%89%87-img-%E7%AD%89%E6%AF%94%E7%BC%A9%E6%94%BE"><span class="nav-number">2.</span> <span class="nav-text">图片 img 等比缩放</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%8B%96%E5%8A%A8%E8%B0%83%E6%95%B4-div-%E5%B0%BA%E5%AF%B8"><span class="nav-number">3.</span> <span class="nav-text">拖动调整 div 尺寸</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%B8%80"><span class="nav-number">3.1.</span> <span class="nav-text">一</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#css-%E6%96%87%E6%9C%AC%E8%B6%85%E5%87%BA%E9%9A%90%E8%97%8F%E5%B9%B6%E6%98%BE%E7%A4%BA%E2%80%A6"><span class="nav-number">4.</span> <span class="nav-text">css 文本超出隐藏并显示…</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%94%9F%E6%88%90%E9%9A%8F%E6%9C%BA%E6%95%B0"><span class="nav-number">5.</span> <span class="nav-text">生成随机数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%9A%8F%E6%9C%BA%E9%A2%9C%E8%89%B2"><span class="nav-number">6.</span> <span class="nav-text">随机颜色</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%85%89%E6%A0%87%E7%A7%BB%E5%8A%A8%E5%88%B0%E5%BC%80%E5%A4%B4%E6%88%96%E7%BB%93%E5%B0%BE"><span class="nav-number">7.</span> <span class="nav-text">光标移动到开头或结尾</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%80%89%E4%B8%AD%E6%89%80%E6%9C%89%E6%96%87%E6%9C%AC%E5%86%85%E5%AE%B9"><span class="nav-number">8.</span> <span class="nav-text">选中所有文本内容</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%88%A4%E6%96%AD%E6%98%AF%E5%90%A6%E6%9C%89%E6%BB%9A%E5%8A%A8%E6%9D%A1"><span class="nav-number">9.</span> <span class="nav-text">判断是否有滚动条</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%8D%B8%E8%BD%BD%E6%89%8B%E6%9C%BA%E9%A2%84%E7%BD%AE%E5%BA%94%E7%94%A8adb"><span class="nav-number">10.</span> <span class="nav-text">卸载手机预置应用adb</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#css-%E7%89%B9%E6%95%88"><span class="nav-number">11.</span> <span class="nav-text">css 特效</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#box-shadow"><span class="nav-number">11.0.1.</span> <span class="nav-text">box-shadow</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%A7%BB%E5%8A%A8%E7%AB%AF"><span class="nav-number">12.</span> <span class="nav-text">移动端</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#dart-sass"><span class="nav-number">13.</span> <span class="nav-text">dart-sass</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%8F%8D%E8%BD%AC%E5%AF%B9%E8%B1%A1"><span class="nav-number">14.</span> <span class="nav-text">反转对象</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#ifram-popstate"><span class="nav-number">15.</span> <span class="nav-text">ifram popstate</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%8E%B7%E5%8F%96%E8%A1%A8%E5%8D%95%E6%95%B0%E6%8D%AE"><span class="nav-number">16.</span> <span class="nav-text">获取表单数据</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%AD%A3%E5%88%99"><span class="nav-number">17.</span> <span class="nav-text">正则</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%8C%B9%E9%85%8D%E6%95%B0%E5%AD%97"><span class="nav-number">17.1.</span> <span class="nav-text">匹配数字</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#github-%E5%AF%B9%E6%AF%94"><span class="nav-number">18.</span> <span class="nav-text">github 对比</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#getCurrentTime"><span class="nav-number">19.</span> <span class="nav-text">getCurrentTime</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%89%93%E4%B9%B1%E6%95%B0%E7%BB%84"><span class="nav-number">20.</span> <span class="nav-text">打乱数组</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%8E%B7%E5%8F%96%E6%8C%87%E5%AE%9A%E8%8C%83%E5%9B%B4%E5%86%85%E7%9A%84%E9%9A%8F%E6%9C%BA%E6%95%B0"><span class="nav-number">21.</span> <span class="nav-text">获取指定范围内的随机数</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%B7%B1%E5%BA%A6%E5%86%BB%E7%BB%93%E5%AF%B9%E8%B1%A1"><span class="nav-number">22.</span> <span class="nav-text">深度冻结对象</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%A6%81%E6%AD%A2%E6%B5%8F%E8%A7%88%E5%99%A8%E5%90%8E%E9%80%80"><span class="nav-number">23.</span> <span class="nav-text">禁止浏览器后退</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-overview">
            <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Xiaolong"
      src="/images/lion.jpg">
  <p class="site-author-name" itemprop="name">Xiaolong</p>
  <div class="site-description" itemprop="description">Never stop learning</div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">45</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">17</span>
        <span class="site-state-item-name">tags</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author site-overview-item animated">
      <span class="links-of-author-item">
        <a href="https://github.com/drwna" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;drwna" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
  </div>



          </div>
        </div>
      </div>
        <div class="back-to-top animated" role="button" aria-label="Back to top">
          <i class="fa fa-arrow-up"></i>
          <span>0%</span>
        </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="reading-progress-bar"></div>

  <a href="https://github.com/Drwna" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="en">
    <link itemprop="mainEntityOfPage" href="http://example.com/2022/03/28/47/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/lion.jpg">
      <meta itemprop="name" content="Xiaolong">
      <meta itemprop="description" content="Never stop learning">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Xiaolong's Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          code
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2022-03-28 22:03:47" itemprop="dateCreated datePublished" datetime="2022-03-28T22:03:47+08:00">2022-03-28</time>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <p>常用代码</p>
<span id="more"></span>

<h2 id="父元素"><a href="#父元素" class="headerlink" title="父元素"></a>父元素</h2><p><a target="_blank" rel="noopener" href="https://segmentfault.com/a/1190000013966650">https://segmentfault.com/a/1190000013966650</a></p>
<h4 id="父元素-1"><a href="#父元素-1" class="headerlink" title="父元素"></a>父元素</h4><figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-tag">div</span>&#123;</span><br><span class="line">	<span class="attribute">width</span>: <span class="number">400px</span>;</span><br><span class="line">	<span class="attribute">height</span>: <span class="number">400px</span>;</span><br><span class="line">	<span class="attribute">position</span>: relative; <span class="comment">/* 使用定位实现水平垂直居中 */</span></span><br><span class="line">&#125;</span><br><span class="line">按父容器宽度自动缩放，并且保持图片原本的长宽比</span><br></pre></td></tr></table></figure>
<h4 id="子元素"><a href="#子元素" class="headerlink" title="子元素"></a>子元素</h4><p>按父容器宽度自动缩放，并且保持图片原本的长宽比</p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-tag">img</span>&#123;</span><br><span class="line">	<span class="attribute">width</span>: auto;</span><br><span class="line">    <span class="attribute">height</span>: auto;</span><br><span class="line">    <span class="attribute">max-width</span>: <span class="number">100%</span>;</span><br><span class="line">    <span class="attribute">max-height</span>: <span class="number">100%</span>;</span><br><span class="line">    <span class="comment">/* 向父元素定位 */</span></span><br><span class="line">    <span class="attribute">position</span>: absolute;</span><br><span class="line">	<span class="attribute">top</span>:<span class="number">50%</span>;</span><br><span class="line">	<span class="attribute">left</span>:<span class="number">50%</span>;</span><br><span class="line">	<span class="attribute">transform</span>: <span class="built_in">translate</span>(-<span class="number">50%</span>,-<span class="number">50%</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="图片-img-等比缩放"><a href="#图片-img-等比缩放" class="headerlink" title="图片 img 等比缩放"></a>图片 img 等比缩放</h2><p>父元素需要指定 宽度或高度</p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-tag">img</span>&#123;</span><br><span class="line">	<span class="attribute">width</span>: auto;</span><br><span class="line">	<span class="attribute">height</span>: auto;</span><br><span class="line">	<span class="attribute">max-width</span>: <span class="number">100%</span>;</span><br><span class="line">	<span class="attribute">max-height</span>: <span class="number">100%</span>;	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<p><a target="_blank" rel="noopener" href="https://blog.csdn.net/ITZ1992/article/details/106908244">https://blog.csdn.net/ITZ1992/article/details/106908244</a></p>
<h2 id="拖动调整-div-尺寸"><a href="#拖动调整-div-尺寸" class="headerlink" title="拖动调整 div 尺寸"></a>拖动调整 div 尺寸</h2><h3 id="一"><a href="#一" class="headerlink" title="一"></a>一</h3><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE <span class="keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span>Title<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">style</span>&gt;</span><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="selector-tag">body</span>, <span class="selector-tag">html</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">width</span>: <span class="number">100%</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">height</span>: <span class="number">100%</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">margin</span>: <span class="number">0</span>;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="selector-id">#container</span> &#123;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">width</span>: <span class="number">200px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">height</span>: <span class="number">200px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">padding</span>: <span class="number">15px</span>;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">box-sizing</span>: border-box;</span></span><br><span class="line"><span class="language-css">            <span class="attribute">border</span>: <span class="number">1px</span> solid black;</span></span><br><span class="line"><span class="language-css">        &#125;</span></span><br><span class="line"><span class="language-css"></span></span><br><span class="line"><span class="language-css">        <span class="selector-class">.item</span> &#123;</span></span><br><span class="line"><span class="language-css">             <span class="attribute">cursor</span>: default;</span></span><br><span class="line"><span class="language-css">             <span class="attribute">width</span>: <span class="number">100%</span>;</span></span><br><span class="line"><span class="language-css">             <span class="attribute">height</span>: <span class="number">100%</span>;</span></span><br><span class="line"><span class="language-css">             <span class="attribute">background</span>: <span class="number">#757575</span>;</span></span><br><span class="line"><span class="language-css">             <span class="attribute">border</span>: <span class="number">1px</span> solid purple;</span></span><br><span class="line"><span class="language-css">         &#125;</span></span><br><span class="line"><span class="language-css">    </span><span class="tag">&lt;/<span class="name">style</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span> <span class="attr">id</span>=<span class="string">&quot;body&quot;</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">id</span>=<span class="string">&quot;container&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">div</span> <span class="attr">class</span>=<span class="string">&quot;item&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">    <span class="comment">//需要调整尺寸的div</span></span></span><br><span class="line"><span class="language-javascript">    <span class="keyword">let</span> c = <span class="variable language_">document</span>.<span class="title function_">getElementById</span>(<span class="string">&#x27;container&#x27;</span>)</span></span><br><span class="line"><span class="language-javascript">    <span class="comment">// body监听移动事件</span></span></span><br><span class="line"><span class="language-javascript">    <span class="variable language_">document</span>.<span class="title function_">getElementById</span>(<span class="string">&#x27;body&#x27;</span>).<span class="title function_">addEventListener</span>(<span class="string">&#x27;mousemove&#x27;</span>, move)</span></span><br><span class="line"><span class="language-javascript">    <span class="comment">// 鼠标按下事件</span></span></span><br><span class="line"><span class="language-javascript">    c.<span class="title function_">addEventListener</span>(<span class="string">&#x27;mousedown&#x27;</span>, down)</span></span><br><span class="line"><span class="language-javascript">    <span class="comment">// 鼠标松开事件</span></span></span><br><span class="line"><span class="language-javascript">    <span class="variable language_">document</span>.<span class="title function_">getElementById</span>(<span class="string">&#x27;body&#x27;</span>).<span class="title function_">addEventListener</span>(<span class="string">&#x27;mouseup&#x27;</span>, up)</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">    <span class="comment">// 是否开启尺寸修改</span></span></span><br><span class="line"><span class="language-javascript">    <span class="keyword">let</span> resizeable = <span class="literal">false</span></span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">    <span class="comment">// 鼠标按下时的坐标，并在修改尺寸时保存上一个鼠标的位置</span></span></span><br><span class="line"><span class="language-javascript">    <span class="keyword">let</span> clientX, clientY</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">    <span class="comment">// div可修改的最小宽高</span></span></span><br><span class="line"><span class="language-javascript">    <span class="keyword">let</span> minW = <span class="number">8</span>, minH = <span class="number">8</span></span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">    <span class="comment">// 鼠标按下时的位置，使用n、s、w、e表示</span></span></span><br><span class="line"><span class="language-javascript">    <span class="keyword">let</span> direc = <span class="string">&#x27;&#x27;</span></span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">    <span class="comment">// 鼠标松开时结束尺寸修改</span></span></span><br><span class="line"><span class="language-javascript">    <span class="keyword">function</span> <span class="title function_">up</span>(<span class="params"></span>) &#123;</span></span><br><span class="line"><span class="language-javascript">        resizeable = <span class="literal">false</span></span></span><br><span class="line"><span class="language-javascript">    &#125;</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">    <span class="comment">// 鼠标按下时开启尺寸修改</span></span></span><br><span class="line"><span class="language-javascript">    <span class="keyword">function</span> <span class="title function_">down</span>(<span class="params">e</span>) &#123;</span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">let</span> d = <span class="title function_">getDirection</span>(e)</span></span><br><span class="line"><span class="language-javascript">        <span class="comment">// 当位置为四个边和四个角时才开启尺寸修改</span></span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">if</span> (d !== <span class="string">&#x27;&#x27;</span>) &#123;</span></span><br><span class="line"><span class="language-javascript">            resizeable = <span class="literal">true</span></span></span><br><span class="line"><span class="language-javascript">            direc = d</span></span><br><span class="line"><span class="language-javascript">            clientX = e.<span class="property">clientX</span></span></span><br><span class="line"><span class="language-javascript">            clientY = e.<span class="property">clientY</span></span></span><br><span class="line"><span class="language-javascript">        &#125;</span></span><br><span class="line"><span class="language-javascript">    &#125;</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">    <span class="comment">// 鼠标移动事件</span></span></span><br><span class="line"><span class="language-javascript">    <span class="keyword">function</span> <span class="title function_">move</span>(<span class="params">e</span>) &#123;</span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">let</span> d = <span class="title function_">getDirection</span>(e)</span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">let</span> cursor</span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">if</span> (d === <span class="string">&#x27;&#x27;</span>) cursor = <span class="string">&#x27;default&#x27;</span>;</span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">else</span> cursor = d + <span class="string">&#x27;-resize&#x27;</span>;</span></span><br><span class="line"><span class="language-javascript">        <span class="comment">// 修改鼠标显示效果</span></span></span><br><span class="line"><span class="language-javascript">        c.<span class="property">style</span>.<span class="property">cursor</span> = cursor;</span></span><br><span class="line"><span class="language-javascript">        <span class="comment">// 当开启尺寸修改时，鼠标移动会修改div尺寸</span></span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">if</span> (resizeable) &#123;</span></span><br><span class="line"><span class="language-javascript">            <span class="comment">// 鼠标按下的位置在右边，修改宽度</span></span></span><br><span class="line"><span class="language-javascript">            <span class="keyword">if</span> (direc.<span class="title function_">indexOf</span>(<span class="string">&#x27;e&#x27;</span>) !== -<span class="number">1</span>) &#123;</span></span><br><span class="line"><span class="language-javascript">                c.<span class="property">style</span>.<span class="property">width</span> = <span class="title class_">Math</span>.<span class="title function_">max</span>(minW, c.<span class="property">offsetWidth</span> + (e.<span class="property">clientX</span> - clientX)) + <span class="string">&#x27;px&#x27;</span></span></span><br><span class="line"><span class="language-javascript">                clientX = e.<span class="property">clientX</span></span></span><br><span class="line"><span class="language-javascript">            &#125;</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">            <span class="comment">// 鼠标按下的位置在上部，修改高度</span></span></span><br><span class="line"><span class="language-javascript">            <span class="keyword">if</span> (direc.<span class="title function_">indexOf</span>(<span class="string">&#x27;n&#x27;</span>) !== -<span class="number">1</span>) &#123;</span></span><br><span class="line"><span class="language-javascript">                c.<span class="property">style</span>.<span class="property">height</span> = <span class="title class_">Math</span>.<span class="title function_">max</span>(minH, c.<span class="property">offsetHeight</span> + (clientY - e.<span class="property">clientY</span>)) + <span class="string">&#x27;px&#x27;</span></span></span><br><span class="line"><span class="language-javascript">                clientY = e.<span class="property">clientY</span></span></span><br><span class="line"><span class="language-javascript">            &#125;</span></span><br><span class="line"><span class="language-javascript">            <span class="comment">// 鼠标按下的位置在底部，修改高度</span></span></span><br><span class="line"><span class="language-javascript">            <span class="keyword">if</span> (direc.<span class="title function_">indexOf</span>(<span class="string">&#x27;s&#x27;</span>) !== -<span class="number">1</span>) &#123;</span></span><br><span class="line"><span class="language-javascript">                c.<span class="property">style</span>.<span class="property">height</span> = <span class="title class_">Math</span>.<span class="title function_">max</span>(minH, c.<span class="property">offsetHeight</span> + (e.<span class="property">clientY</span> - clientY)) + <span class="string">&#x27;px&#x27;</span></span></span><br><span class="line"><span class="language-javascript">                clientY = e.<span class="property">clientY</span></span></span><br><span class="line"><span class="language-javascript">            &#125;</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">            <span class="comment">// 鼠标按下的位置在左边，修改宽度</span></span></span><br><span class="line"><span class="language-javascript">            <span class="keyword">if</span> (direc.<span class="title function_">indexOf</span>(<span class="string">&#x27;w&#x27;</span>) !== -<span class="number">1</span>) &#123;</span></span><br><span class="line"><span class="language-javascript">                c.<span class="property">style</span>.<span class="property">width</span> = <span class="title class_">Math</span>.<span class="title function_">max</span>(minW, c.<span class="property">offsetWidth</span> + (clientX - e.<span class="property">clientX</span>)) + <span class="string">&#x27;px&#x27;</span></span></span><br><span class="line"><span class="language-javascript">                clientX = e.<span class="property">clientX</span></span></span><br><span class="line"><span class="language-javascript">            &#125;</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">        &#125;</span></span><br><span class="line"><span class="language-javascript">    &#125;</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">    <span class="comment">// 获取鼠标所在div的位置</span></span></span><br><span class="line"><span class="language-javascript">    <span class="keyword">function</span> <span class="title function_">getDirection</span>(<span class="params">ev</span>) &#123;</span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">let</span> xP, yP, offset, dir;</span></span><br><span class="line"><span class="language-javascript">        dir = <span class="string">&#x27;&#x27;</span>;</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">        xP = ev.<span class="property">offsetX</span>;</span></span><br><span class="line"><span class="language-javascript">        yP = ev.<span class="property">offsetY</span>;</span></span><br><span class="line"><span class="language-javascript">        offset = <span class="number">10</span>;</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">if</span> (yP &lt; offset) dir += <span class="string">&#x27;n&#x27;</span>;</span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">else</span> <span class="keyword">if</span> (yP &gt; c.<span class="property">offsetHeight</span> - offset) dir += <span class="string">&#x27;s&#x27;</span>;</span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">if</span> (xP &lt; offset) dir += <span class="string">&#x27;w&#x27;</span>;</span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">else</span> <span class="keyword">if</span> (xP &gt; c.<span class="property">offsetWidth</span> - offset) dir += <span class="string">&#x27;e&#x27;</span>;</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">return</span> dir;</span></span><br><span class="line"><span class="language-javascript">    &#125;</span></span><br><span class="line"><span class="language-javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h2 id="css-文本超出隐藏并显示…"><a href="#css-文本超出隐藏并显示…" class="headerlink" title="css 文本超出隐藏并显示…"></a>css 文本超出隐藏并显示…</h2><p>一行内容</p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">overflow</span>: hidden;</span><br><span class="line"><span class="attribute">text-overflow</span>: ellipsis;</span><br><span class="line"><span class="attribute">white-space</span>: nowrap;</span><br></pre></td></tr></table></figure>

<p>两行内容</p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">overflow</span>: hidden;</span><br><span class="line"><span class="attribute">text-overflow</span>: ellipsis;</span><br><span class="line"><span class="attribute">display</span>:-webkit-box; //作为弹性伸缩盒子模型显示。</span><br><span class="line">-webkit-box-orient:vertical; //设置伸缩盒子的子元素排列方式--从上到下垂直排列</span><br><span class="line">-webkit-line-clamp:<span class="number">2</span>; //显示的行</span><br></pre></td></tr></table></figure>



<h2 id="生成随机数"><a href="#生成随机数" class="headerlink" title="生成随机数"></a>生成随机数</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="title class_">Math</span>.<span class="title function_">random</span>().<span class="title function_">toString</span>(<span class="number">36</span>).<span class="title function_">slice</span>(-<span class="number">4</span>)</span><br></pre></td></tr></table></figure>





<h2 id="随机颜色"><a href="#随机颜色" class="headerlink" title="随机颜色"></a>随机颜色</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*******************************************************************</span></span><br><span class="line"><span class="comment"> *生成随机颜色</span></span><br><span class="line"><span class="comment"> *********************************************</span></span><br><span class="line"><span class="comment"> * randomColor1():返回10进制颜色值，例如：#167772</span></span><br><span class="line"><span class="comment"> * randomColor2():返回16进制颜色值，例如：#ffffff</span></span><br><span class="line"><span class="comment"> * randomColor3():返回rgb格式颜色值，例如：rgb(255,255,255)</span></span><br><span class="line"><span class="comment"> *</span></span><br><span class="line"><span class="comment"> *********************************************************************/</span></span><br><span class="line"><span class="keyword">static</span> <span class="title function_">randomColor1</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&#x27;#&#x27;</span> + <span class="title class_">Math</span>.<span class="title function_">floor</span>(<span class="title class_">Math</span>.<span class="title function_">random</span>() * <span class="number">256</span>).<span class="title function_">toString</span>(<span class="number">10</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">static</span> <span class="title function_">randomColor2</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&#x27;#&#x27;</span> + <span class="title class_">Math</span>.<span class="title function_">floor</span>(<span class="title class_">Math</span>.<span class="title function_">random</span>() * <span class="number">0xffffff</span>).<span class="title function_">toString</span>(<span class="number">16</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">static</span> <span class="title function_">randomColor3</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">let</span> r = <span class="title class_">Math</span>.<span class="title function_">floor</span>(<span class="title class_">Math</span>.<span class="title function_">random</span>() * <span class="number">256</span>);</span><br><span class="line">    <span class="keyword">let</span> g = <span class="title class_">Math</span>.<span class="title function_">floor</span>(<span class="title class_">Math</span>.<span class="title function_">random</span>() * <span class="number">256</span>);</span><br><span class="line">    <span class="keyword">let</span> b = <span class="title class_">Math</span>.<span class="title function_">floor</span>(<span class="title class_">Math</span>.<span class="title function_">random</span>() * <span class="number">256</span>);</span><br><span class="line">    <span class="keyword">return</span> <span class="string">&quot;rgb(&quot;</span> + r + <span class="string">&#x27;,&#x27;</span> + g + <span class="string">&#x27;,&#x27;</span> + b + <span class="string">&quot;)&quot;</span>;</span><br><span class="line">     </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="光标移动到开头或结尾"><a href="#光标移动到开头或结尾" class="headerlink" title="光标移动到开头或结尾"></a>光标移动到开头或结尾</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// jquery</span></span><br><span class="line">$(<span class="variable language_">this</span>).<span class="title function_">focus</span>();</span><br><span class="line"><span class="keyword">let</span> selector = <span class="variable language_">window</span>.<span class="title function_">getSelection</span>();</span><br><span class="line">selector.<span class="title function_">collapse</span>($(<span class="variable language_">this</span>)[<span class="number">0</span>], <span class="number">1</span>);</span><br></pre></td></tr></table></figure>



<h2 id="选中所有文本内容"><a href="#选中所有文本内容" class="headerlink" title="选中所有文本内容"></a>选中所有文本内容</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> () =&gt; &#123;</span><br><span class="line">  <span class="comment">// 监听鼠标的click事件</span></span><br><span class="line">  <span class="variable language_">document</span>.<span class="title function_">addEventListener</span>(<span class="string">&#x27;click&#x27;</span>, <span class="function">(<span class="params">e: Event</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="comment">// 获取到鼠标点击的dom元素</span></span><br><span class="line">    <span class="keyword">const</span> span = e.<span class="property">target</span>;</span><br><span class="line">    <span class="comment">// 获取dom元素中的的innerText的值</span></span><br><span class="line">    <span class="keyword">const</span> value = (span <span class="keyword">as</span> <span class="title class_">HTMLElement</span>).<span class="property">innerText</span>;</span><br><span class="line">    <span class="comment">// 遍历所有配置的正则表达式</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="keyword">const</span> reg <span class="keyword">of</span> <span class="title class_">Object</span>.<span class="title function_">values</span>(regObject)) &#123;</span><br><span class="line">      <span class="keyword">if</span> (reg.<span class="title function_">test</span>(value)) &#123;</span><br><span class="line">        <span class="comment">// 验证通过了正则的值是否是span元素或者是div元素，因为不想在输入框也有此功能</span></span><br><span class="line">        <span class="keyword">if</span> ((span <span class="keyword">as</span> <span class="title class_">HTMLElement</span>).<span class="property">localName</span> === (<span class="string">&#x27;span&#x27;</span> || <span class="string">&#x27;div&#x27;</span>)) &#123;</span><br><span class="line">            </span><br><span class="line">            <span class="comment">// 核心代码</span></span><br><span class="line">          <span class="comment">// 获取一个Selection对象</span></span><br><span class="line">          <span class="keyword">const</span> selection = <span class="variable language_">window</span>.<span class="title function_">getSelection</span>();</span><br><span class="line">          <span class="comment">// 先清空一下以选择的内容</span></span><br><span class="line">          selection?.<span class="title function_">removeAllRanges</span>();</span><br><span class="line">          <span class="comment">// 创建一个range对象</span></span><br><span class="line">          <span class="keyword">const</span> range = <span class="variable language_">document</span>.<span class="title function_">createRange</span>();</span><br><span class="line">          <span class="comment">// 让range对象包含一个node的内容</span></span><br><span class="line">          range.<span class="title function_">selectNodeContents</span>(span <span class="keyword">as</span> <span class="title class_">HTMLElement</span>);</span><br><span class="line">          <span class="comment">// 向selection中添加一个区域（range）</span></span><br><span class="line">          selection?.<span class="title function_">addRange</span>(range);</span><br><span class="line">            </span><br><span class="line">        &#125;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;);</span><br><span class="line">&#125;;</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h2 id="判断是否有滚动条"><a href="#判断是否有滚动条" class="headerlink" title="判断是否有滚动条"></a>判断是否有滚动条</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">hasScrollbar</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="variable language_">document</span>.<span class="property">body</span>.<span class="property">scrollHeight</span> &gt; (<span class="variable language_">window</span>.<span class="property">innerHeight</span> || <span class="variable language_">document</span>.<span class="property">documentElement</span>.<span class="property">clientHeight</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><a target="_blank" rel="noopener" href="https://www.cnblogs.com/nzbin/p/8117535.html">https://www.cnblogs.com/nzbin/p/8117535.html</a></p>
<h2 id="卸载手机预置应用adb"><a href="#卸载手机预置应用adb" class="headerlink" title="卸载手机预置应用adb"></a>卸载手机预置应用adb</h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">// 确保成功连接</span><br><span class="line">adb devices</span><br><span class="line"></span><br><span class="line">//点击手机上的应用，产看控制台输出的应用包</span><br><span class="line">adb shell am monitor</span><br><span class="line"></span><br><span class="line">//卸载</span><br><span class="line">adb shell pm uninstall --user -0 xxx</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<center><img src="https://cdn.jsdelivr.net/gh/Drwna/image//images/Snipaste_2022-04-27_21-08-49.png" alt="Snipaste_2022-04-27_21-08-49" width="800px"></center>



















<h2 id="css-特效"><a href="#css-特效" class="headerlink" title="css 特效"></a>css 特效</h2><h4 id="box-shadow"><a href="#box-shadow" class="headerlink" title="box-shadow"></a>box-shadow</h4><p><a target="_blank" rel="noopener" href="http://www.corelangs.com/css/box/image-shadow.html">http://www.corelangs.com/css/box/image-shadow.html</a></p>
<p>推荐：</p>
<p><a target="_blank" rel="noopener" href="https://getcssscan.com/css-box-shadow-examples">https://getcssscan.com/css-box-shadow-examples</a></p>
<h2 id="移动端"><a href="#移动端" class="headerlink" title="移动端"></a>移动端</h2><p>去除 a 标签点击样式</p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-tag">a</span>&#123;</span><br><span class="line"><span class="attribute">outline</span>:none；</span><br><span class="line">-webkit-tap-highlight-color: <span class="built_in">rgba</span>(<span class="number">0</span>,<span class="number">0</span>,<span class="number">0</span>,<span class="number">0</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>







<h2 id="dart-sass"><a href="#dart-sass" class="headerlink" title="dart-sass"></a>dart-sass</h2><p>安装后：</p>
<p>sass –watch scss/:css/</p>
<p>实时监听 将 scss 目录下的 scss 文件编译为 css 文件并输入到 css 目录下</p>
<h2 id="反转对象"><a href="#反转对象" class="headerlink" title="反转对象"></a>反转对象</h2><p><a target="_blank" rel="noopener" href="https://stackoverflow.com/questions/23013573/swap-key-with-value-in-object">https://stackoverflow.com/questions/23013573/swap-key-with-value-in-object</a></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> <span class="title function_">objectFlip</span> = (<span class="params">obj</span>) =&gt; &#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="title class_">Object</span>.<span class="title function_">keys</span>(obj).<span class="title function_">reduce</span>(<span class="function">(<span class="params">ret, key</span>) =&gt;</span> &#123;</span><br><span class="line">    ret[obj[key]] = key</span><br><span class="line">    <span class="keyword">return</span> ret</span><br><span class="line">  &#125;, &#123;&#125;)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//</span></span><br><span class="line"><span class="keyword">function</span> <span class="title function_">swap</span>(<span class="params">json</span>)&#123;</span><br><span class="line">  <span class="keyword">var</span> ret = &#123;&#125;;</span><br><span class="line">  <span class="keyword">for</span>(<span class="keyword">var</span> key <span class="keyword">in</span> json)&#123;</span><br><span class="line">    ret[json[key]] = key;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> ret;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h2 id="ifram-popstate"><a href="#ifram-popstate" class="headerlink" title="ifram popstate"></a>ifram popstate</h2><p>页面中有 iframe 时浏览器的返回按钮失效</p>
<p>浏览器返回作用在 iframe 页面上。</p>
<p>监听浏览器的 popstate 事件失效。是因为刷新了 iframe，iframe.src = iframe.src</p>
<p>要刷新 iframe 的话重新创建 iframe 元素即可。</p>
<h2 id="获取表单数据"><a href="#获取表单数据" class="headerlink" title="获取表单数据"></a>获取表单数据</h2><p>获取多选框</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">方法一：</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> formData = <span class="keyword">new</span> <span class="title class_">FormData</span>($s(<span class="string">&#x27;form&#x27;</span>));</span><br><span class="line"><span class="keyword">const</span> formDataObj = &#123;</span><br><span class="line">    <span class="attr">artwork</span>: formData.<span class="title function_">getAll</span>(<span class="string">&#x27;artwork&#x27;</span>),</span><br><span class="line">    <span class="attr">highlight</span>: formData.<span class="title function_">getAll</span>(<span class="string">&#x27;music&#x27;</span>),</span><br><span class="line">    <span class="attr">music</span>: formData.<span class="title function_">getAll</span>(<span class="string">&#x27;highlight&#x27;</span>),</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// console.log(formDataObj);</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">方法二：</span><br><span class="line"><span class="comment">// 获取form中checkbox的值</span></span><br><span class="line"><span class="keyword">const</span> formCheckbox = $s(<span class="string">&#x27;form&#x27;</span>).<span class="title function_">querySelectorAll</span>(<span class="string">&#x27;input[type=&quot;checkbox&quot;]&#x27;</span>);</span><br><span class="line"><span class="keyword">const</span> formCheckboxList = <span class="title class_">Array</span>.<span class="property"><span class="keyword">prototype</span></span>.<span class="property">slice</span>.<span class="title function_">call</span>(formCheckbox);</span><br><span class="line"><span class="keyword">const</span> formCheckboxListByName = &#123;&#125;;</span><br><span class="line">formCheckboxList.<span class="title function_">forEach</span>(<span class="keyword">function</span> (<span class="params">item</span>) &#123;</span><br><span class="line">    <span class="keyword">const</span> name = item.<span class="property">name</span>;</span><br><span class="line">    <span class="keyword">if</span> (!formCheckboxListByName[name]) &#123;</span><br><span class="line">        formCheckboxListByName[name] = [];</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">if</span> (item.<span class="property">checked</span>) &#123;</span><br><span class="line">        formCheckboxListByName[name].<span class="title function_">push</span>(item.<span class="property">value</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;)</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>





<h2 id="正则"><a href="#正则" class="headerlink" title="正则"></a>正则</h2><h3 id="匹配数字"><a href="#匹配数字" class="headerlink" title="匹配数字"></a>匹配数字</h3><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">s.<span class="title function_">match</span>(<span class="regexp">/^\d+$/</span>)</span><br></pre></td></tr></table></figure>



<h2 id="github-对比"><a href="#github-对比" class="headerlink" title="github 对比"></a>github 对比</h2><p><code>加参数 &amp;w=1</code></p>
<h2 id="getCurrentTime"><a href="#getCurrentTime" class="headerlink" title="getCurrentTime"></a>getCurrentTime</h2><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="attr">getCurrentTime</span>: <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">const</span> now = <span class="keyword">new</span> <span class="title class_">Date</span>();</span><br><span class="line">    <span class="keyword">const</span> year = now.<span class="title function_">getFullYear</span>();</span><br><span class="line">    <span class="keyword">const</span> month = now.<span class="title function_">getMonth</span>() + <span class="number">1</span>;</span><br><span class="line">    <span class="keyword">const</span> day = now.<span class="title function_">getDate</span>();</span><br><span class="line">    <span class="keyword">const</span> hour = now.<span class="title function_">getHours</span>();</span><br><span class="line">    <span class="keyword">const</span> minute = now.<span class="title function_">getMinutes</span>();</span><br><span class="line">    <span class="keyword">const</span> second = now.<span class="title function_">getSeconds</span>();</span><br><span class="line">    <span class="keyword">return</span> year + <span class="string">&#x27;-&#x27;</span> + month + <span class="string">&#x27;-&#x27;</span> + day + <span class="string">&#x27; &#x27;</span> + hour + <span class="string">&#x27;:&#x27;</span> + minute + <span class="string">&#x27;:&#x27;</span> + second;</span><br><span class="line">&#125;,</span><br><span class="line"></span><br></pre></td></tr></table></figure>





<h2 id="打乱数组"><a href="#打乱数组" class="headerlink" title="打乱数组"></a>打乱数组</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> <span class="title function_">randomWord</span> = (<span class="params">word</span>) =&gt;</span><br><span class="line">   word</span><br><span class="line">     .<span class="title function_">split</span>(<span class="string">&#x27;&#x27;</span>)</span><br><span class="line">     .<span class="title function_">sort</span>(<span class="function">() =&gt;</span> <span class="title class_">Math</span>.<span class="title function_">random</span>() - <span class="number">0.5</span>)</span><br><span class="line">     .<span class="title function_">join</span>(<span class="string">&#x27;&#x27;</span>);</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h2 id="获取指定范围内的随机数"><a href="#获取指定范围内的随机数" class="headerlink" title="获取指定范围内的随机数"></a>获取指定范围内的随机数</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> <span class="title function_">getRandomByRange</span> = (<span class="params">start: number, end: number</span>) =&gt; <span class="title class_">Math</span>.<span class="title function_">round</span>(<span class="title class_">Math</span>.<span class="title function_">random</span>() * (end - start) + start);</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<h2 id="深度冻结对象"><a href="#深度冻结对象" class="headerlink" title="深度冻结对象"></a>深度冻结对象</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> <span class="title function_">deepFreeze</span> = obj =&gt; &#123;</span><br><span class="line">  <span class="title class_">Object</span>.<span class="title function_">keys</span>(obj).<span class="title function_">forEach</span>(<span class="function"><span class="params">prop</span> =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">if</span> (<span class="keyword">typeof</span> obj[prop] === <span class="string">&#x27;object&#x27;</span> &amp;&amp; !<span class="title class_">Object</span>.<span class="title function_">isFrozen</span>(obj[prop])) <span class="title function_">deepFreeze</span>(obj[prop]);</span><br><span class="line">  &#125;);</span><br><span class="line">  <span class="keyword">return</span> <span class="title class_">Object</span>.<span class="title function_">freeze</span>(obj);</span><br><span class="line">&#125;;</span><br><span class="line"><span class="comment">// 使用</span></span><br><span class="line"><span class="keyword">const</span> myObj = &#123;</span><br><span class="line">  <span class="attr">a</span>: <span class="number">1</span>,</span><br><span class="line">  <span class="attr">b</span>: <span class="string">&#x27;hello&#x27;</span>,</span><br><span class="line">  <span class="attr">c</span>: [<span class="number">0</span>, <span class="number">1</span>, <span class="number">2</span>],</span><br><span class="line">  <span class="attr">d</span>: &#123; <span class="attr">e</span>: <span class="number">1</span>, <span class="attr">f</span>: <span class="number">2</span> &#125;</span><br><span class="line">&#125;;</span><br><span class="line"><span class="title function_">deepFreeze</span>(myObj)</span><br></pre></td></tr></table></figure>





<h2 id="禁止浏览器后退"><a href="#禁止浏览器后退" class="headerlink" title="禁止浏览器后退"></a>禁止浏览器后退</h2><figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable language_">window</span>.<span class="property">history</span>.<span class="title function_">pushState</span>(<span class="literal">null</span>, <span class="literal">null</span>, <span class="variable language_">window</span>.<span class="property">location</span>.<span class="property">href</span>);</span><br><span class="line"><span class="variable language_">window</span>.<span class="property">onpopstate</span> = <span class="keyword">function</span> (<span class="params"></span>) &#123;</span><br><span class="line">    <span class="variable language_">window</span>.<span class="property">history</span>.<span class="title function_">go</span>(<span class="number">1</span>);</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>




























    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/js/" rel="tag"><i class="fa fa-tag"></i> js</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2022/03/04/45/" rel="prev" title="test">
                  <i class="fa fa-chevron-left"></i> test
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2022/12/01/00/" rel="next" title="Canvas">
                  Canvas <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 2021 – 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Xiaolong</span>
</div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script>

  
<script src="/js/third-party/search/local-search.js"></script>




  





</body>
</html>
